package com.example.searchservice.controller;

import com.example.searchservice.doc.StudentDocument;
import com.example.searchservice.service.impl.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/students")
@Slf4j
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    // 创建用户
    @PostMapping
    public ResponseEntity<String> createStudentDocument(@RequestBody StudentDocument student) {
        try {
            String studentId = studentService.createOrUpdateStudent(student);
            return ResponseEntity.ok(studentId);
        } catch (IOException e) {
            log.error("创建用户失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 根据ID获取用户
    @GetMapping("/{id}")
    public ResponseEntity<StudentDocument> getStudentDocumentById(@PathVariable String id) {
        try {
            StudentDocument student = studentService.getStudentById(id);
            if (student != null) {
                return ResponseEntity.ok(student);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (IOException e) {
            log.error("获取用户失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 搜索用户
    @GetMapping("/search")
    public ResponseEntity<List<StudentDocument>> searchStudentDocuments(@RequestParam String q) {
        try {
            List<StudentDocument> students = studentService.searchStudents(q);
            return ResponseEntity.ok(students);
        } catch (IOException e) {
            log.error("搜索用户失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 根据名称搜索用户
    @GetMapping("/search/name")
    public ResponseEntity<List<StudentDocument>> searchByName(@RequestParam String name) {
        try {
            List<StudentDocument> students = studentService.searchByName(name);
            return ResponseEntity.ok(students);
        } catch (IOException e) {
            log.error("按名称搜索用户失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 获取所有用户
    @GetMapping
    public ResponseEntity<List<StudentDocument>> getAllStudentDocuments() {
        try {
            List<StudentDocument> students = studentService.getAllStudents();
            return ResponseEntity.ok(students);
        } catch (IOException e) {
            log.error("获取所有用户失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStudentDocument(@PathVariable String id) {
        try {
            boolean deleted = studentService.deleteStudent(id);
            if (deleted) {
                return ResponseEntity.ok().build();
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (IOException e) {
            log.error("删除用户失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 检查索引状态
    @GetMapping("/index/status")
    public ResponseEntity<Boolean> checkIndexStatus() {
        try {
            boolean exists = studentService.indexExists();
            return ResponseEntity.ok(exists);
        } catch (IOException e) {
            log.error("检查索引状态失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    // 创建索引
    @PostMapping("/index/create")
    public ResponseEntity<Boolean> createIndex() {
        try {
            boolean created = studentService.createIndex();
            return ResponseEntity.ok(created);
        } catch (IOException e) {
            log.error("创建索引失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
}